package com.cysion.study.ui.fragment

import android.app.Activity
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.other.color
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.uibox.dialog.CONFIRM
import com.cysion.study.R
import com.cysion.study.adapter.PendingAdapter
import com.cysion.study.entity.ApprovalEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box.context
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.presenter.PendingPresenter
import com.cysion.study.ui.iview.PendingView
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_myapproval.*
import kotlinx.android.synthetic.main.activity_pending.*
import kotlinx.android.synthetic.main.activity_pending.fl_load_state
import kotlinx.android.synthetic.main.activity_pending.multiView
import kotlinx.android.synthetic.main.activity_pending.rv_list
import kotlinx.android.synthetic.main.activity_pending.smartLayout
import kotlinx.android.synthetic.main.activity_pending.topbar
import kotlinx.android.synthetic.main.fragment_pending.*

//待审批
class PendingActivity : BaseActivity(), PendingView {


    //绑定presenter
    private val presenter by lazy {
        PendingPresenter().apply {
            attach(this@PendingActivity)
        }
    }

    private var mdatalist: MutableList<ApprovalEntity> = mutableListOf()
    private lateinit var pendingAdapter: PendingAdapter
    private var curPage = 1
    private var category = 0//0.待我审核 1.抄送我的
    override fun getLayoutId(): Int = R.layout.activity_pending

    override fun initView() {
        darkTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("我的审批")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()

    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            presenter.getList(curPage, 0)
            smartLayout.setEnableLoadMore(true)
            fl_load_state.visibility = View.GONE
        }
        smartLayout.setOnLoadMoreListener {
            fl_load_state.visibility = View.GONE
            presenter.getList(curPage, 0)
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        pendingAdapter = PendingAdapter(mdatalist, context)
        rv_list.adapter = pendingAdapter
        rv_list.layoutManager = LinearLayoutManager(context)
        pendingAdapter.setOnTypeClickListener { obj, position, flag ->
            //flag -1 为审批明细弹窗事件
            if (flag == -1) {
                Alert.detail(context as Activity, "", "确认删除这个博客吗？") { type, msg ->
                    if (type == CONFIRM) {
                        ShowToast("明细")
                    }
                }
            } else {
                //同意或拒绝审批
                noticeRead(obj.tid, obj.remark, flag)
            }
        }
    }

    private fun noticeRead(tid: String, remark: String, flag:Int) {
        // flag1.同意 2.拒绝
        ApiClient.api.approvalDeal(
            UserCache.token, tid, remark,flag.toString()
        ).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    ShowToast("操作成功")
                    presenter.getList(curPage, 0)
                    Alert.close()
                }
                _onError {
                    //                        Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    override fun onGetPendingList(pendingList: MutableList<ApprovalEntity>) {
        if (curPage == 1) {
            mdatalist.clear()
        }

        val index = mdatalist.size

        /*if (index == 0) {
            pendingAdapter.notifyDataSetChanged()
        } else {
            pendingAdapter.notifyItemRangeChanged(index, 10)
        }*/
        if (pendingList.size <= 0) {
            multiView.showEmpty()
        } else {
            curPage++
            mdatalist.addAll(pendingList)
            pendingAdapter.notifyDataSetChanged()
            multiView.showContent()
        }
    }

    override fun loading() {
        Alert.loading(this)
    }

    override fun stopLoad() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
        Alert.close()
    }

    override fun error(code: Int, msg: String) {
        ShowToast(msg)
    }

    override fun closeMvp() {
        presenter.detach()
    }

}
